console.log(location.search);
var obj = new URLSearchParams(location.search);
console.log(obj);
var pid = obj.get("id")
console.log(pid);
//   接口地址：http://jx.xuzhixiang.top/ap/api/detail.php
// 接口请求方式：get
// 接口参数：
// id  商品的id
itemset()

function itemset() {
    console.log(pid);
    var itemApi = `http://jx.xuzhixiang.top/ap/api/detail.php`;
    let p = axios.get(itemApi, {
        params: {
            id: pid
        }
    }).then(r => {
        console.log(r.data);
        var pObj = r.data.data;
        console.log(pObj);
        let str = `
            <div>
            <img src="${pObj.pimg}" alt="" />
            </div>
        `;
        con.innerHTML = str
        countDown();
        setInterval(countDown, 1000)
        function countDown() {
            var oldtimes = +new Date()
            var newtimes = +new Date("2022-6-31 18:00:00")
            var times = (newtimes - oldtimes) / 1000;
            var d = parseInt(times / 60 / 60 / 24)
            d = d < 10 ? "0 " + d : d;
            var h = parseInt(times / 60 / 60 % 24);
            h = h < 10 ? '0' + h : h; //补0
            var m = parseInt(times /60 % 60);
            m = m < 10 ? '0' + m : m;
            var s = parseInt(times % 60);
            s = s < 10 ? '0' + s : s;
            let str2 = `
            <h1>${pObj.pname}</h1>
            <p style="font-size:18px ;font-weight:bold ;display:inline-block ;height:70px;line-height:70px;">￥：${pObj.pprice}</p>
            <p style="background-color:#ED3029;">抢购剩余时间：${d}天${h}小时${m}分${s}秒</p>
            
            `
            rightcon1.innerHTML = str2;
        }
        let str4 = ` <input type="number" value='1' min="1" class="pnum"><button onclick="addCartHandle()">加入购物车</button>`
       rightcon2.innerHTML = str4

    })
};

function addCartHandle() {
    console.log("点击加入了购物车");
    //发起请求
    let uid = localStorage.getItem("id");
    let pnum = document.querySelector(".pnum").value;
    let url = "http://jx.xuzhixiang.top/ap/api/add-product.php";
    axios
        .get(url, {
            params: {
                uid,
                pid,
                pnum,
            },
        })
        .then((r) => {
            //加入购物车成功
            console.log(r.data);
            alert("加入购物车成功")
        });
}
var user = document.querySelector(".user")
window.onload = function () {
    let username = localStorage.getItem("username")
    str1 = `欢迎您，用户${username}`
    user.innerHTML = str1
}
var cart = document.querySelector(".cart")
cart.onclick = function () {
    location.href = "mycar.html"
}
shouye.onclick = function () {
    location.href = "list.html"
}
// 接口地址：http://jx.xuzhixiang.top/ap/api/cart-list.php
// 接口请求方式：get
// 接口参数：
//      id  用户id

// 使用方式
//      获取id为1的用户的购物车
//      http://jx.xuzhixiang.top/ap/api/cart-list.php?id=1

// 服务器返回json数据
let uid = localStorage.getItem("id");

let cartnumAPI = `http://jx.xuzhixiang.top/ap/api/cart-list.php?id=${uid}`
let p3 = axios.get(cartnumAPI, { params: { id: uid } }).then(r => {
    console.log("数据");
    console.log(r.data.data.length);
    let str3 = `${r.data.data.length}`
    goodsnum.innerHTML = str3
})